package org.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import org.dk.domain.DataResult;
import org.dk.em.DataResultEnum;
import org.dk.pojo.TbContentCategory;
import org.dk.service.ConCateService;
import org.dk.service.TbContentCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("conCateController")
@Controller
public class ConCateController {

    @Reference
    private ConCateService conCateService;

    @Reference
    private TbContentCategoryService categoryService;

    @RequestMapping("toContentCategory")
    public String toContentCategory(){
        return "admin/content_category";
    }

    @RequestMapping("selectByName")
    @ResponseBody
    public DataResult selectByName(String name){
        try {
           List<TbContentCategory> list = categoryService.selectByName(name);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("addConCat")
    @ResponseBody
    public DataResult addConCat(TbContentCategory category){
        try {
            conCateService.addConCat(category);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteConCat")
    @ResponseBody
    public DataResult deleteConCat(@RequestParam("ids[]") Long[] ids){
        try {
            conCateService.deleteConCat(ids);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
    @RequestMapping("updateConCatReturn")
    @ResponseBody
    public DataResult updateConCatReturn(Long id){
        try {
            TbContentCategory category = conCateService.updateConCatReturn(id);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS).setData(category);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }

    @RequestMapping("updateConCat")
    @ResponseBody
    public DataResult updateConCat(TbContentCategory category){
        try {
            conCateService.updateConCat(category);
            return DataResult.buildDataResult(DataResultEnum.SUCCESS);
        }catch (Exception e){
            return DataResult.buildDataResult(DataResultEnum.ERROR);
        }
    }
}
